package com.taomu.app.zongchouhuli.moudle.home.contract.impl;

import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResMainProductListEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.moudle.home.contract.HomeProductMoreContract;
import com.taomu.app.zongchouhuli.moudle.index.entity.ProductEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class HomeProductMorePresenter implements HomeProductMoreContract.Presenter {

    private HomeProductMoreContract.View mContractView;

    private CompositeDisposable disposable;

    private String productname;

    private String order = "";

    private String desc = "";

    private String ship = "";

    private String take = "";

    public HomeProductMorePresenter(HomeProductMoreContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getTimeSearch(String state) {
        this.order = "id";
        this.ship = "0";
        if("down".equals(state)){
            state = "up";
            this.desc = "desc";
        }else{
            state = "down";
            this.desc = "asc";
        }
        if(mContractView != null){
            //todo:测试数据，不添加
            mContractView.setTimeResults(null,state);
        }
        setMoreProduct();
    }

    @Override
    public void getIncomeSearch(String state) {
        this.order = "month_income";
        this.ship = "0";
        if("down".equals(state)){
            state = "up";
            this.desc = "desc";
        }else{
            state = "down";
            this.desc = "asc";
        }
        if(mContractView != null){
            //todo:测试数据，不添加
            mContractView.setIncomeResults(null,state);
        }
        setMoreProduct();
    }

    @Override
    public void getMoreProductList(String name,String ship,String take) {
        this.productname = name;
        this.ship = ship;
        this.take = take;
        setMoreProduct();
    }

    @Override
    public void detch() {

    }

    private void setMoreProduct(){
        Request.getIntence().getProductListData(productname,ship,take,order,desc)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResMainProductListEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMainProductListEntity resProductEntities) {
                        if(mContractView != null){
                            mContractView.setMorePorductList(resProductEntities);
                        }
                    }

                });
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
    }
}
